package com.example.content.views.im;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.content.R;
import com.hyphenate.easeui.widget.EaseChatInputMenu;
import com.hyphenate.easeui.widget.EaseChatPrimaryMenuBase;
import com.hyphenate.easeui.widget.chatextend.EaseChatExtendMenu;
import com.hyphenate.util.EMLog;

/**
 * primary menu
 */
public class ChatPrimaryMenu extends EaseChatPrimaryMenuBase implements OnClickListener {
    private EditText editText;
    private View sendImage;
    private ImageView sendMsg;
    private boolean ctrlPress = false;
    private ImageView sendEmoji;
    private EaseChatExtendMenu.EaseChatExtendMenuItemClickListener photoClickListener;

    public ChatPrimaryMenu(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }

    public ChatPrimaryMenu(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChatPrimaryMenu(Context context) {
        super(context);
        init(context, null);
    }

    private void init(final Context context, AttributeSet attrs) {
        Context context1 = context;
        LayoutInflater.from(context).inflate(R.layout.chat_primary_menu, this);
        editText = (EditText) findViewById(R.id.message);
        sendImage = findViewById(R.id.send_photo);
        sendMsg = findViewById(R.id.send_msg);
        sendEmoji = (ImageView) findViewById(R.id.send_emoji);

        sendMsg.setOnClickListener(this);
        sendImage.setOnClickListener(this);
        sendEmoji.setOnClickListener(this);
        editText.setOnClickListener(this);
        editText.requestFocus();

        editText.setOnFocusChangeListener(new OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (listener != null) {
                    listener.onEditTextHasFocus(hasFocus);
                }
            }
        });
        // listen the text change
        editText.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (!TextUtils.isEmpty(s)) {
                    sendMsg.setClickable(true);
                    sendMsg.setImageResource(R.drawable.ic_send_msg_enabled);
                } else {
                    sendMsg.setClickable(false);
                    sendMsg.setImageResource(R.drawable.ic_send_msg);
                }
                if (listener != null) {
                    listener.onTyping(s, start, before, count);
                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        editText.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                EMLog.d("key", "keyCode:" + keyCode + " action:" + event.getAction());

                // test on Mac virtual machine: ctrl map to KEYCODE_UNKNOWN
                if (keyCode == KeyEvent.KEYCODE_UNKNOWN) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        ctrlPress = true;
                    } else if (event.getAction() == KeyEvent.ACTION_UP) {
                        ctrlPress = false;
                    }
                }
                return false;
            }
        });

        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                EMLog.i("EaseChatPrimaryMenu", "actionId: " + actionId);
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER &&
                                event.getAction() == KeyEvent.ACTION_DOWN &&
                                ctrlPress == true)) {
                    String s = editText.getText().toString();
                    editText.setText("");
                    listener.onSendBtnClicked(s);
                    return true;
                } else {
                    return false;
                }
            }
        });
    }

    public void setPhotoClickListener(EaseChatExtendMenu.EaseChatExtendMenuItemClickListener photoClickListener) {
        this.photoClickListener = photoClickListener;
    }

    /**
     * append emoji icon to editText
     *
     * @param emojiContent
     */
    public void onEmojiconInputEvent(CharSequence emojiContent) {
        editText.append(emojiContent);
    }

    /**
     * delete emojicon
     */
    public void onEmojiconDeleteEvent() {
        if (!TextUtils.isEmpty(editText.getText())) {
            KeyEvent event = new KeyEvent(0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
            editText.dispatchKeyEvent(event);
        }
    }

    @Override
    public void onExtendMenuContainerHide() {

    }

    /**
     * on clicked event
     *
     * @param view
     */
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.send_msg) {
            if (listener != null) {
                String s = editText.getText().toString();
                editText.setText("");
                if (!s.trim().isEmpty()) {
                    listener.onSendBtnClicked(s);
                }
            }
        } else if (id == R.id.send_photo) {
            if (photoClickListener != null) {
                photoClickListener.onChatExtendMenuItemClick(EaseChatInputMenu.ITEM_PICTURE, view);
            }
        } else if (id == R.id.message) {
            if (listener != null)
                listener.onEditTextClicked();
        } else if (id == R.id.send_emoji) {
            if (listener != null) {
                listener.onToggleEmojiconClicked();
            }
        }
    }


    @Override
    public void onTextInsert(CharSequence text) {
        int start = editText.getSelectionStart();
        Editable editable = editText.getEditableText();
        editable.insert(start, text);
    }

    @Override
    public EditText getEditText() {
        return editText;
    }

    @Override
    public void setHint(String hint) {
        super.setHint(hint);
        if (editText != null) {
            editText.setHint(hint);
        }
    }

    @Override
    public String getInputContent() {
        if (editText == null) {
            return "";
        }
        return editText.getText().toString().trim();
    }
}
